package com.dxb.yomall.product.controller;

import java.util.Arrays;
import java.util.Map;

import com.dxb.common.exception.BizCode;
import com.dxb.common.valid.AddGroup;
import com.dxb.common.valid.UpdateGroup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.dxb.yomall.product.entity.BrandEntity;
import com.dxb.yomall.product.service.BrandService;
import com.dxb.common.utils.PageUtils;
import com.dxb.common.utils.R;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;


/**
 * Ʒ
 *
 * @author daixinbao
 * @email 1065808217@qq.com
 * @date 2021-01-05 22:26:07
 */
@RestController
@RequestMapping("product/brand")
public class BrandController {
    @Autowired
    private BrandService brandService;

    /**
     * 列表
     */
    @RequestMapping("/list")
        public R list(@RequestParam Map<String, Object> params){
        PageUtils page = brandService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{brandId}")
        public R info(@PathVariable("brandId") Long brandId){
		BrandEntity brand = brandService.getById(brandId);

        return R.ok().put("brand", brand);
    }

    /**
     * 保存 // 新增使用 AddGroup 分组校验
     */
    @RequestMapping("/save")
        public R save(@Validated({AddGroup.class})@RequestBody BrandEntity brand){
		brandService.save(brand);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
        public R update(@Validated({UpdateGroup.class})@RequestBody BrandEntity brand){
		//brandService.updateById(brand);
        brandService.updateCascade(brand);
        return R.ok();
    }

    /**
     * 更新状态
     * @param params
     * @return
     */
    @RequestMapping("/updateStatus")
    public R updateStatus(@RequestBody Map<String, Object> params){
        //TODO 优化代码
        String brandId=String.valueOf(params.get("brandId"));
        if(StringUtils.isEmpty(brandId)) return R.error(BizCode.VALID_EXCEPTION.getCode(),BizCode.VALID_EXCEPTION.getMessage())
                                                 .put("data","brandId不能为空");
        String showStatus=String.valueOf(params.get("showStatus"));
        if(StringUtils.isEmpty(showStatus)) return R.error(BizCode.VALID_EXCEPTION.getCode(),BizCode.VALID_EXCEPTION.getMessage())
                .put("data","showStatus不能为空");
        brandService.updateStatusById(brandId,showStatus);
        return R.ok();
    }
    /**
     * 删除
     */
    @RequestMapping("/delete")
        public R delete(@RequestBody Long[] brandIds){
		brandService.removeByIds(Arrays.asList(brandIds));

        return R.ok();
    }

}
